package search.binarysearch;

/**
 * @author XY
 * @version 1.0
 * @date 2022/3/2 9:32
 * @Description
 */
public class Leet69 {
    public static int mySqrt(int x) {
        int l = 0;
        int r = x;
        int temp = 0;
        while(l<=r){
            int mid = l+(r-l)/2;
            if((long) mid*mid<=x){
                temp = mid;
                l = mid + 1;
            }else{
                r = mid - 1;
            }

        }
        return temp;
    }

    //牛顿迭代法,难。。。。
    public static int mySqrt1(int x) {
        int l = 0;
        int r = x;
        int temp = 0;
        while(l<=r){
            int mid = l+(r-l)/2;
            if((long) mid*mid<=x){
                temp = mid;
                l = mid + 1;
            }else{
                r = mid - 1;
            }

        }
        return temp;
    }


    public static void main(String[] args) {
        System.out.println(mySqrt(2147395599));
    }


}
